import axios from 'axios'
import { MessageBox, Message, Loading } from 'element-ui'
import store from '@/store'
import { getToken } from '@/utils/auth'

// create an axios instance
const service = axios.create({
  baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
  // withCredentials: true, // send cookies when cross-domain requests
  timeout: 20000 // request timeout
})
// const loadingInstance = ''
// const options = {
//   lock: true,
//   text: 'Loading',
//   spinner: 'el-icon-loading',
//   background: 'rgba(0, 0, 0, 0.7)'
// }
// request interceptor
service.interceptors.request.use(
  config => {
    // 调用loading
    // loadingInstance = Loading.service(options)
    // do something before request is sent

    if (store.getters.token) {
      // let each request carry token
      // ['Airport-Token'] is a custom headers key
      // please modify it according to the actual situation
      config.headers['sso_admin_sessionid'] = getToken()
      config.headers['sso_system_flag'] = 23
    }
    return config
  },
  error => {
    // do something with request error
    console.log(error) // for debug
    return Promise.reject(error)
  }
)

// response interceptor
service.interceptors.response.use(
  /**
   * If you want to get http information such as headers or status
   * Please return  response => response
   */

  /**
   * Determine the request status by custom code
   * Here is just an example
   * You can also judge the status by HTTP Status Code
   */
  response => {
    // loadingInstance.close()
    const res = response.data
    if (response.request.responseType && response.request.responseType === 'blob') {
      return response
    }

    if (res.code != '200') {
      Message.closeAll()
      Message({
        message: res.message || res.msg || 'Error',
        type: 'error',
        duration: 2 * 1000
      })

      // 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired;
      // if (res.code == '401') {
      //   // to re-login
      //   MessageBox.confirm('您已退出登录，您可以取消以留在此页面，或重新登录  ', '确认注销  ', {
      //     confirmButtonText: '重新登录',
      //     cancelButtonText: '取 消',
      //     type: 'warning'
      //   }).then(() => {
      //     store.dispatch('user/resetToken').then(() => {
      //       location.reload()
      //     })
      //   })
      // }
      // return Promise.reject(res.message || res.msg || 'Error')
    } else {
      return res
    }
  },
  error => {
    // if (error.response && error.response.status == '401') {
    //   // to re-login
    //   MessageBox.confirm('您已退出登录，您可以取消以留在此页面，或重新登录  ', '确认注销  ', {
    //     confirmButtonText: '重新登录',
    //     cancelButtonText: '取 消',
    //     type: 'warning'
    //   }).then(() => {
    //     store.dispatch('user/resetToken').then(() => {
    //       location.reload()
    //     })
    //   })
    // }
    // loadingInstance.close()
    Message.closeAll()
    Message({
      message:
        (error.response && error.response.message) ||
        (error.response && error.response.data && error.response.data.msg) ||
        'Error',
      type: 'error',
      duration: 5 * 1000
    })
    return Promise.reject(error)
  }
)

export default service
